<template>
  <div class="account">
    <edu-page-tabs :tab-list="xTabs" />
    <div>
      <el-row type="flex" align="middle" class="hander">
        <span class="title">用户资料</span>
        <el-link type="primary" :underline="false" @click="$router.push({ path: '/user/student/' + $route.params.id + '/edit' })">编辑</el-link>
      </el-row>
      <div class="account-data">
        <el-row type="flex" class="account-info">
          <el-avatar shape="square" :size="70" :src="response.user.avatar_url" />
          <div class="box">
            <div class="name">{{ response.user.nick_name || response.user.phone }}</div>
            <div class="info">{{ response.user.signature || '未填写' }}</div>
          </div>
        </el-row>
        <el-row :gutter="100" class="account-list">
          <el-col :span="8">
            <el-row type="flex" class="item-col">
              <span class="label">
                手机号
                <i />
              </span>
              <span class="text">{{ response.user.phone }}</span>
            </el-row>
          </el-col>
          <el-col :span="8">
            <el-row type="flex" class="item-col">
              <span class="label">
                用户名
                <i />
              </span>
              <span class="text">{{ response.user.user_name }}</span>
            </el-row>
          </el-col>
          <el-col :span="8">
            <el-row type="flex" class="item-col">
              <span class="label">
                性别
                <i />
              </span>
              <span v-if="response.user.gender == 1" class="text">男</span>
              <span v-else-if="response.user.gender == 2" class="text">女</span>
              <span v-else class="text">保密</span>
            </el-row>
          </el-col>
          <el-col :span="8">
            <el-row type="flex" class="item-col">
              <span class="label">
                注册来源
                <i />
              </span>
              <span class="text">{{ response.user.register_device_text }}</span>
            </el-row>
          </el-col>
          <el-col :span="8">
            <el-row type="flex" class="item-col">
              <span class="label">
                账号状态
                <i />
              </span>
              <span v-if="response.user.status == 1" class="text green">{{ response.user.status_text }}</span>
              <span v-else class="text red">{{ response.user.status_text }}</span>
            </el-row>
          </el-col>
          <el-col :span="8">
            <el-row type="flex" class="item-col">
              <span class="label">
                余额
                <i />
              </span>
              <span class="text">{{ userBalance | moneySymbol }}</span>
              <i class="el-icon-edit el-icon--right" @click="updateBalance" />
            </el-row>
          </el-col>
          <el-col :span="8">
            <el-row type="flex" class="item-col">
              <span class="label">
                收入
                <i />
              </span>
              <span class="text">{{ response.user.income | moneySymbol }}</span>
            </el-row>
          </el-col>
          <el-col :span="8">
            <el-row type="flex" class="item-col">
              <span class="label">
                积分
                <i />
              </span>
              <span class="text">{{ userCredit }}</span>
              <i class="el-icon-edit el-icon--right" @click="updateCredit" />
            </el-row>
          </el-col>
        </el-row>
      </div>
    </div>
    <div class="record">
      <el-tabs v-model="record">
        <el-tab-pane label="学习概况" name="survey" lazy><survey :datas="response.survey" /></el-tab-pane>
        <el-tab-pane label="在学课程" name="record" lazy><record /></el-tab-pane>
        <el-tab-pane label="购买记录" name="buy" lazy><buy /></el-tab-pane>
        <el-tab-pane label="考试记录" name="exam" lazy><exam /></el-tab-pane>
        <el-tab-pane label="证书记录" name="cert" lazy><cert /></el-tab-pane>
      </el-tabs>
    </div>
    <balance :visible.sync="visibleBalance" :user-id="userId" :balance="userBalance" @update:balance="setBalance" />
    <integral :visible.sync="visibleIntegral" :user-id="userId" :credit="userCredit" @update:credit="setCredit" />
  </div>
</template>

<script>
import EduPageTabs from '@public/template/tabs/page_tabs.vue'
import Survey from './study/survey.vue'
import Record from './study/record.vue'
import Buy from './study/buy.vue'
import Exam from './study/exam'
import Cert from './study/cert'

import Balance from './components/balance.vue'
import Integral from './components/integral.vue'

export default {
  components: {
    EduPageTabs,
    Survey,
    Record,
    Buy,
    Exam,
    Cert,
    Balance,
    Integral
  },
  data() {
    return {
      response: {
        user: {},
        survey: {}
      },
      xTabs: [
        {
          title: '学员列表',
          uri: '/user/student',
          meta: {
            permission: 'user.student.list'
          }
        },
        {
          title: '学习情况',
          uri: '/user/student/' + this.$route.params.id + '/study',
          meta: {
            permission: 'user.student.detail'
          }
        }
      ],
      record: 'survey',
      visibleBalance: false,
      visibleIntegral: false
    };
  },
  computed: {
    userBalance() {
      return this.response.user.balance;
    },
    userCredit() {
      return this.response.user.credit;
    },
    userId() {
      return this.$route.params.id;
    }
  },
  created() {
    this.$http.get(
      '/user/admin/account/' + this.$route.params.id + '/study',
      {},
      response => {
        this.response = response.data;
      },
      _ => {},
      false
    );
  },
  mounted() {
    this.$store.dispatch('setLoading', 0);
  },
  methods: {
    // 更新余额
    updateBalance() {
      this.visibleBalance = true;
    },
    // 设置余额
    setBalance(val) {
      this.response.user.balance = val;
    },
    // 更新积分弹出层
    updateCredit() {
      this.visibleIntegral = true;
    },
    // 设置积分
    setCredit(val) {
      this.response.user.credit = val
    }
  }
};
</script>
<style scoped lang="scss">
.account {
  .hander {
    border-bottom: 1px solid #ebeef5;
    padding: 0 20px;
    .title {
      font-size: 14px;
      font-weight: bold;
      color: #303133;
      margin-right: 6px;
      border-bottom: 2px solid #5496fc;
      padding: 10px 0;
      margin-bottom: -1px;
    }
    .el-link {
      font-size: 12px;
    }
  }
  .account-data {
    padding: 20px 20px;
    .account-info {
      .box {
        padding: 10px 0;
        margin-left: 10px;
      }
      .name {
        color: #303133;
        font-size: 15px;
        font-weight: bold;
      }
      .info {
        color: #909399;
        font-size: 12px;
        margin-top: 20px;
      }
    }
    .account-list {
      .label {
        color: #606266;
        font-size: 12px;
        width: 48px;
        text-align: justify;
        i {
          display: inline-block;
          width: 100%;
        }
      }
      .green {
        color: #67c23a !important;
      }
      .red {
        color: #f56c6c !important;
      }

      .text {
        color: #909399;
        font-size: 12px;
        margin-left: 12px;
      }
      .item-col {
        margin-top: 25px;
      }
    }
  }
  .record {
    padding: 0 20px;
  }
}
</style>
